package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.util.CommonUtils;
import com.szewec.ps.util.StringUtils;

import java.util.Arrays;
import java.util.List;

public class TwoInputItem extends ConstraintLayout implements BaseInput {
    protected TextView itemName;
    protected EditText value2;
    protected EditText value3;
    protected TextView tvShow;
    protected ConstraintLayout container;

    public TwoInputItem(Context context) {
        super(context);
    }

    public TwoInputItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public TwoInputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.two_input_item_view, this);
        itemName = findViewById(R.id.tv_item_name);
        value2 = findViewById(R.id.et_2);
        value3 = findViewById(R.id.et_3);
        tvShow = findViewById(R.id.tv_show);
        container = findViewById(R.id.cl_container);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItem);
            String name = ta.getString(R.styleable.InputItem_itemName);
            itemName.setText(name);

            boolean required = ta.getBoolean(R.styleable.InputItem_required, false);

            if (!required) {
                itemName.setCompoundDrawables(null, null, null, null);
            }

            ta.recycle();
        }
    }

    public String getStringValue(){
        String v2 = value2.getText().toString();
        String v3 = value3.getText().toString();
        if (StringUtils.isEmpty(v2) && StringUtils.isEmpty(v3) ){
            return null;
        }
        return v2 + Constants.CONNECTOR + v3;
    }


//    public void setValue(String value){
//        if (StringUtils.isNotEmpty(value) && value.contains(Constants.CONNECTOR)){
//            List<String> vals = Arrays.asList(value.split(Constants.CONNECTOR));
//            if (vals.size() == 2){
//                value2.setText(vals.get(0));
//                value3.setText(vals.get(1));
//            }
//            tvShow.setText(CommonUtils.formatAngle(value));
//        }
//    }


    @Override
    public void switchoverMode(boolean isEnabled) {
        value2.setEnabled(isEnabled);
        value3.setEnabled(isEnabled);
        if (isEnabled){
            container.setVisibility(VISIBLE);
            tvShow.setVisibility(GONE);
            value2.setHint(R.string.input_hint);
            value3.setHint(R.string.input_hint);
        }else {
            container.setVisibility(GONE);
            tvShow.setVisibility(VISIBLE);
            value2.setHint(R.string.empty_string);
            value3.setHint(R.string.empty_string);
        }

    }
}
